<template>
  <div class="app-container">
    <el-form :model="searchForm" ref="searchFormRef" size="small" inline v-show="showSearch">
      <el-form-item label="菜单名称" prop="name">
        <el-input v-model="searchForm.name" placeholder="请输入菜单名称" clearable/>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="searchForm.status" placeholder="菜单状态" clearable class="w-120px">
          <el-option v-for="dd in dict.type.sys_normal_disable" :key="dd.value" :label="dd.label" :value="dd.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="mb-8px reset">
      <el-button type="primary" plain icon="el-icon-plus" @click="handleAdd" v-hasPerm="['system:menu:add']">新增</el-button>
      <el-button type="info" plain icon="el-icon-sort" @click="handleExpand">展开/折叠</el-button>
      <right-toolbar :showSearch.sync="showSearch" @refresh="getList" />
    </div>

    <el-table v-if="refreshTable" v-loading="loading" :data="listData" row-key="id" :default-expand-all="isExpandAll">
      <el-table-column prop="name" label="菜单名称" show-overflow-tooltip width="160" />
      <el-table-column prop="icon" label="图标" align="center" width="60">
        <template v-slot="{ row }"><icon-show :icon="row.icon" /></template>
      </el-table-column>
      <el-table-column prop="perms" label="权限标识" show-overflow-tooltip />
      <el-table-column prop="component" label="组件路径" show-overflow-tooltip />
      <el-table-column prop="list_sort" label="排序" align="center" width="60" />
      <el-table-column prop="status" label="状态" align="center" width="60">
        <template v-slot="{ row }">
          <dict-tag :options="dict.type.sys_normal_disable" :value="row.status" />
        </template>
      </el-table-column>
      <el-table-column prop="is_visible" label="显示" align="center" width="70">
        <template v-slot="{ row }">
          <dict-tag :options="dict.type.sys_show_hide" :value="row.is_visible" />
        </template>
      </el-table-column>
      <el-table-column prop="create_time" label="创建时间" align="center" width="140">
        <template v-slot="{ row }"> {{ parseTime(row.create_time) }}</template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width" width="160">
        <template v-slot="{ row }">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleEdit(row)" v-hasPerm="['system:menu:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-plus" @click="handleAdd(row)" v-hasPerm="['system:menu:add']">新增</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(row)" v-hasPerm="['system:menu:delete']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <MenuForm v-model="formShow" :row="formData" v-if="formShow" @submit="getList" />
  </div>
</template>

<script>
import { listMenuApi, delMenuApi } from '@/api/system/menu'
import MenuForm from './form.vue'

export default {
  name: 'Menu',
  dicts: ['sys_normal_disable', 'sys_show_hide'],
  components: { MenuForm },
  data() {
    return {
      loading: true,
      showSearch: true,
      listData: [],
      menuOptions: [],
      isExpandAll: false,
      refreshTable: true,
      searchForm: {
        name: undefined,
        status: undefined
      },
      //添加修改弹窗
      formShow: false,
      formData: null
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      listMenuApi(this.searchForm).then(res => {
        this.listData = this.listToTree(res?.list || [])
        this.loading = false
      })
    },
    search() {
      this.getList()
    },
    reset() {
      this.resetForm('searchFormRef')
      this.search()
    },
    handleExpand() {
      this.refreshTable = false
      this.isExpandAll = !this.isExpandAll
      this.$nextTick(() => {
        this.refreshTable = true
      })
    },
    handleAdd(row) {
      this.formData = { parent_id: row?.id || '' }
      this.formShow = true
    },
    handleEdit(row) {
      this.formData = 'id' in row ? row : null
      if (!this.formData) return this.$modal.msgError('参数缺失')
      this.formShow = true
    },
    handleDelete(row) {
      const menuId = row?.id || ''
      if (!menuId) return this.$modal.msgWarning('未选择数据')
      this.$modal.confirm('是否确认删除名称为"' + row.name + '"的数据项？').then(() => {
        return delMenuApi(row.id)
      }).then((res) => {
        this.getList()
        this.$modal.msgSuccess(res.msg || '删除成功')
      }).catch(() => {})
    }
  }
}
</script>
